package br.unifor.mia.graph.setorcensitario;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

@Entity
public class Node {

	private Integer code;

	@Id
	private String name;
	
	@OneToMany(mappedBy = "sector")
	@LazyCollection(LazyCollectionOption.EXTRA)
	private Set<Link> sectorLinks = new HashSet<Link>();
	
	@OneToMany(mappedBy = "user")
	@LazyCollection(LazyCollectionOption.EXTRA)
	private Set<Link> userLinks = new HashSet<Link>();
	
	public Node() {
		// Default Constructor, used by Hibernate.
	}

	public Node(Integer code, String name) {
		super();
		this.name = name;
		this.code = code;
	}

	public Integer getCode() {
		return code;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @return the degree
	 */
	public int getDegree() {
		return getSectorDegree() + getUserDegree();
	}
	
	/**
	 * @return the degree
	 */
	public int getSectorDegree() {
		return sectorLinks.size();
	}

	public int getNumberOfCrimesInSector() {
		
		int crimes = 0;
		
		for (Link link : sectorLinks) {
			crimes += link.getValue().size();
		}
		
		return crimes;
	}

	public int getNumberOfCrimesByUser() {
		
		int crimes = 0;
		
		for (Link link : userLinks) {
			crimes += link.getValue().size();
		}
		
		return crimes;
	}
	
	public int getNumberOfCrimes() {
		return getNumberOfCrimesInSector() + getNumberOfCrimesByUser();
	}
	
	/**
	 * @return the degree
	 */
	public int getUserDegree() {
		return userLinks.size();
	}

	/**
	 * @return the links
	 */
	public Set<Link> getSectorLinks() {
		return sectorLinks;
	}

	public void addSectorLink(Link link) {
		sectorLinks.add(link);
	}

	/**
	 * @return the links
	 */
	public Set<Link> getUserLinks() {
		return userLinks;
	}

	public void addUserLink(Link link) {
		userLinks.add(link);
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((getName() == null) ? 0 : getName().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Node)) {
			return false;
		}
		Node other = (Node) obj;
		if (getName() == null) {
			if (other.getName() != null) {
				return false;
			}
		} else if (!getName().equals(other.getName())) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "Node [" + getName() + "]";
	}

	/**
	 * Metodo usado quando o Node for um Setor.
	 * Se o Node for um Usuario esse metodo retorna ZERO. 
	 * 
	 * @param user
	 * @return
	 */
	public int getNumberOfCrimesOf(Node user) {
		
		for (Link link : sectorLinks) {
			if (link.getUser().equals(user)) {
				return link.getValue().size();
			}
		}
		
		return 0;
	}
}